--战术电台
local zhanshudiantai = fk.CreateSkill {
    name = "mcdh_zhanshudiantai"
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

zhanshudiantai:addEffect(fk.TargetSpecifying,{
    can_trigger = function(self, event, target, player, data)
            return target == player and player:hasSkill(self) and
                    player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and data.card.is_damage_card
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        room:setPlayerMark(player, self.name, data.card.number)
        local cards = Card:getIdList(data.card)
        local name = data.card.name
        local success, dat = room:askForUseActiveSkill(player, "#mcdh_zhanshudiantai", "#mcdh_zhanshudiantai-use:::",
                    true, { card_sub = cards, card_name = name })
        if success then
            self.cost_data = dat.targets
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local logic = room.logic
            local card = data.card
            local useCardIds = card:isVirtual() and card.subcards or { card.id }
            room:sendLog{
                type = "#mcdh_zhanshudiantai_log",
                from = player.id,
                to = self.cost_data,
                arg= data.card:toLogString()
            }
            local cancel = AimGroup:getAllTargets(data.tos)
            for _, to in ipairs(cancel) do
                AimGroup:cancelTarget(data, to)
            end
            for _, to in ipairs(self.cost_data) do
                AimGroup:addTargets(room, data, to)
            end
            data.extra_data = data.extra_data or {}
            data.extra_data.mcdh_zhanshudiantaiUser = player.id
    end
})

zhanshudiantai:addEffect(fk.CardUseFinished,{
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
            return  (data.extra_data or {}).mcdh_zhanshudiantaiUser == player.id
    end,
    on_cost = function(self, event, target, player, data)
        return true
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local logic = room.logic
            local num = 0
            logic:getActualDamageEvents(999, function(e)
                if (e:findParent(GameEvent.UseCard).data[1].extra_data or {}).mcdh_zhanshudiantaiUser then
                    local data = e.data[1]
                    num = num + data.damage
                end
                return false
            end, Player.HistoryPhase)
            player:drawCards(num, self.name)
    end
})

Fk:loadTranslationTable {
    ["mcdh_zhanshudiantai"] = "战术电台",
    [":mcdh_zhanshudiantai"] = "出牌阶段限一次，当你使用伤害牌指定目标时，你可以将目标改为体力值之和不大于此牌点数的任意名角色，此牌结算后，你摸等同于造成伤害数值的牌。",
    ["#mcdh_zhanshudiantai_log"] = "%from 将 %arg的目标改为 %to",
    ["#mcdh_zhanshudiantai-use"] = "战术电台:将目标改为体力值之和不大于此牌点数的任意名角色"
}

return zhanshudiantai